package pers.qtong.tank;

import pers.qtong.tank.cor.ColliderChain;

import java.awt.*;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class GameModel{
    Tank myTank;

    List<GameObject> objects = new ArrayList<GameObject>();

    ColliderChain chain = new ColliderChain();

    private static GameModel INSTANCE = new GameModel();

    static{
        INSTANCE.init();
    }

    public static GameModel getInstance() {
        return INSTANCE;
    }

    private GameModel() {}

    private void init() {

        myTank = new Tank(200, 400, Dir.DOWN, Group.GOOD);
        //单例模式
        int tankCount = PropertiesMgr.getInstance().getInt("initTankCount");

        //初始化敌方坦克
        for(int i = 0; i < tankCount; i++) {
//            add(new Tank(50 + i * 80, 200, Dir.DOWN, Group.BAD));
            new Tank(50 + i * 80, 200, Dir.DOWN, Group.BAD);
        }

        new Wall(150, 150, 200, 50);
        new Wall(550, 150, 200, 50);
        new Wall(300, 300, 50, 200);
        new Wall(550, 300, 50, 200);
       /* add(new Wall(150, 150, 200, 50));
        add(new Wall(550, 150, 200, 50));
        add(new Wall(330, 300, 50, 200));
        add(new Wall(550, 300, 50, 200));*/
    }

    public void add(GameObject gameObject) {
        objects.add(gameObject);
    }

    public void remove(GameObject gameObject) {
        objects.remove(gameObject);
    }

    public void paint(Graphics g) {
        Color color = g.getColor();
        g.setColor(Color.white);
        g.setColor(color);
        for(int i = 0; i < objects.size(); i++) {
            objects.get(i).paint(g);
        }
        //碰撞
        for(int i = 0; i < objects.size(); i++) {
            for(int j = i + 1; j < objects.size(); j++) {
                GameObject o1 = objects.get(i);
                GameObject o2 = objects.get(j);
                chain.collide(o1, o2);
            }
        }
    }

    public Tank getMainTank() {
        return myTank;
    }

    public void save() {
        File file = new File("D:/tank.data");
        ObjectOutputStream objectOutputStream = null;
        try {
            objectOutputStream = new ObjectOutputStream(new FileOutputStream(file));
            objectOutputStream.writeObject(myTank);
            objectOutputStream.writeObject(objects);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(objectOutputStream != null) {
                try {
                    objectOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    public void load() {
        File file = new File("D:/tank.data");
        ObjectInputStream objectInputStreams = null;
        try {
            objectInputStreams = new ObjectInputStream(new FileInputStream(file));
            myTank = (Tank)objectInputStreams.readObject();
            objects = (List)objectInputStreams.readObject();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(objectInputStreams != null) {
                try {
                    objectInputStreams.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    private boolean stop = false;
    public void stop() {
    }
}
